package com.gitee.sparta.core.bean.cluster;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.netty.channel.Channel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.nio.channels.SocketChannel;
import java.util.Collection;


/**
 * 集群通讯协议
 *
 * @author zyred
 * @since v 0.0.2
 */
@Setter
@Getter
@NoArgsConstructor
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
public class ClusterProtocol {

    /**
     * see {@link ClusterCmd}
     */
    private byte cmd;

    /**
     * 服务注册 id
     */
    private int serverId;

    /** leader server id **/
    private int leaderSid;

    @JsonIgnore
    private SocketChannel cli;

    @JsonIgnore
    private Channel channel;

    public ClusterProtocol(byte cmd, int serverId, int leaderSid) {
        this.cmd = cmd;
        this.serverId = serverId;
        this.leaderSid = leaderSid;
    }

    public ClusterProtocol(byte cmd, int serverId, int leaderSid, Channel channel) {
        this.cmd = cmd;
        this.serverId = serverId;
        this.leaderSid = leaderSid;
        this.channel = channel;
    }

    public ClusterProtocol(byte cmd, int serverId, int leaderSid, SocketChannel cli) {
        this.cmd = cmd;
        this.serverId = serverId;
        this.leaderSid = leaderSid;
        this.cli = cli;
    }

    /** leader 搬运完毕的 topics **/
    private Collection<String> topics;

    public ClusterProtocol(byte cmd, int serverId, int leaderSid, Collection<String> topics) {
        this.cmd = cmd;
        this.serverId = serverId;
        this.leaderSid = leaderSid;
        this.topics = topics;
    }


    public static ClusterProtocol defaultValue(byte code) {
        return new ClusterProtocol(code, -1, -1);
    }
}
